class Solution
{
public:
    vector<int> avoidFlood(vector<int> &rains)
    {
        int n = rains.size();
        set<int> sunnyDays;
        unordered_map<int, int> lakeLastRain;
        vector<int> answer(n);
        for (int i = 0; i < n; ++i)
        {
            if (rains[i] != 0)
            {
                answer[i] = -1;
                if (lakeLastRain.count(rains[i]))
                {
                    int lastRainDay = lakeLastRain[rains[i]];
                    auto sunnyDay = sunnyDays.lower_bound(lastRainDay);
                    if (sunnyDay == sunnyDays.end())
                    {
                        return {};
                    }
                    else
                    {
                        answer[*sunnyDay] = rains[i];
                        sunnyDays.erase(sunnyDay);
                    }
                }
                lakeLastRain[rains[i]] = i;
            }
            else
            {
                answer[i] = 1;
                sunnyDays.insert(i);
            }
        }
        return answer;
    }
};